package myJs.user.mission

import japgolly.scalajs.react.ReactDOM
import myJs.Utils._
import myJs.myPkg.jquery.$

import scala.scalajs.js.Dynamic.{global => g}
import myJs.tool.Pojo._
import myJs.tool.Tool
import myJs.user.mission.plot._
import org.scalajs.dom.ext.Ajax

import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import myJs.Implicits._
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._

import scala.scalajs.js.JSON
import shared.Pojo._
import upickle.default._
import myJs.projectCps._
import org.scalajs.dom.document

/**
 * Created by yz on 2019/4/25
 */
@JSExportTopLevel("ParCorResult")
object ParCorResult extends MissionResult {

  val formId = "form"

  @JSExport("init")
  def init = {
    refreshMissionInfo(getMissionId)
    refreshFileInfo(getMissionId)
    registerEvent

    selfInit

    //    val selectTabId = EnrichBar.name

    //        val inTab = "Layout"
    val inTab = "Font"
    //    val inTab = "Color"

    //    Tool.selectTab(s"${selectTabId}")
    //    Tool.selectArgTab(s"${selectTabId}${inTab}")

  }

  def selfInit = {
    corHeatmap.Cp.Props().render.renderIntoDOM(document.querySelector("#corHeatmap"))
    val missionId = getMissionId
    val url = g.jsRoutes.controllers.MissionController.getConfigData().url.toString
    Ajax.get(url = s"${url.noCache}&missionId=${missionId}", headers = Map("Content-Type" -> "application/json")).map { xhr =>
      val rs = xhr.responseText.toJDJAn
      val data = read[SharedParCorData](JSON.stringify(rs))
      val onlyNode1 = if (data.hasExtraDataFile) {
        false
      } else true
      CorNetworkTab.init(onlyNode1 = onlyNode1)
    }
  }

  def getMissionId = {
    g.missionId.toString
  }

}
